package com.yj.jason.jsondemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.yj.jason.baselibrary.view.MyRecyclerView.MySwipeRefreshLayout;
import com.yj.jason.baselibrary.view.MyRecyclerView.SwipeMenuRecyclerView;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.CommonRecyclerAdapter;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.MuiltTypeSupport;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.ViewHolder;
import com.yj.jason.baselibrary.view.banner.BannerAdapter;
import com.yj.jason.baselibrary.view.banner.BannerView;
import com.yj.jason.moudlelibrary.activity.BaseSkinActivity;
import com.yj.jason.moudlelibrary.view.DefaultNavigation;

import java.util.ArrayList;
import java.util.List;

public class BannerViewActivity extends BaseSkinActivity {

    private TextView tv_main;
    private ImageView iv_image;
    private SwipeMenuRecyclerView recyclerView;

    private List<String>datas=new ArrayList<>();
    private MySwipeRefreshLayout refresh_layout;

    private BannerView banner_vp;


    //把dip 转成px
    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner_view);
        tv_main=(TextView)findViewById(R.id.tv_main);
        iv_image=(ImageView) findViewById(R.id.iv_image);
        recyclerView=(SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        refresh_layout=(MySwipeRefreshLayout) findViewById(R.id.refresh_layout);


       // refresh_layout.setRefreshing(true);

       /* refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });*/

        for(int i=0;i<13;i++){
            datas.add("我是"+i);
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));


       // recyclerView.addRefreshViewCreator(new DefaultRefreshCreator());

      //  recyclerView.addLoadViewCreator(new DefaultLoadViewCreator());

        recyclerView.useDefaultLoadMore();

        recyclerView.loadMoreFinish(false, true);


       // recyclerView.addItemDecoration(new LinearLayoutItemDecoration(this,R.drawable.item_divider_01));

        //   recyclerView.setAutoLoadMore(true);

        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        datas.add("加载更多数据");
                        recyclerView.loadMoreFinish(false, true);
                          recyclerView.getOriginAdapter().notifyDataSetChanged();
                    }
                },1000);


            }
        });




        recyclerView.setAdapter(new CommonRecyclerAdapter<String>(this, datas, new MuiltTypeSupport<String>() {
            @Override
            public int getLayoutId(String item) {
               // int Rom=new Random().nextInt(2);
                return R.layout.item_main;
            }
        }) {
            @Override
            public void convert(final ViewHolder holder, String item) {
                holder.setText(R.id.tv_item,item);


               /* holder.setOnIntemClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       *//* datas.remove(holder.getAdapterPosition());
                        recyclerView.getAdapter().notifyDataSetChanged();*//*
                    }
                });*/

            }

        });




        final View headerView= LayoutInflater.from(this).inflate(R.layout.header_view,recyclerView,false);

        recyclerView.addHeaderView(headerView);





        BannerView banner_vp = (BannerView) headerView.findViewById(R.id.banner_vp);


        //2、通过Resources获取
        DisplayMetrics dm = getResources().getDisplayMetrics();

        int width=dm.widthPixels;

        banner_vp.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,width));


        banner_vp.setAdapter(new BannerAdapter() {
            @Override
            public View getView(int position) {
                ImageView iv=new ImageView(BannerViewActivity.this);
                iv.setImageResource(R.drawable.ic_launcher);
                return iv;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public String getBannerDesc(int mCurrentPostion) {
                return "2222";
            }
        });


        banner_vp.startRoll();




       /* headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recyclerView.removeHeaderView(headerView);
            }
        });*/

       /* recyclerView.setAdapter(new CommonRecyclerAdapter<String>(this,datas,R.layout.item_main) {
            @Override
            public void convert(ViewHolder holder, String item) {
                holder.setText(R.id.tv_item,item);
            }

        });*/

      //  recyclerView.addItemDecoration(new GirdLayoutItemDecoration(this,R.drawable.item_divider_01));


        tv_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*AlertDialog dialog=new AlertDialog.Builder(MainActivity.this).setContentView(R.layout.default_dialog)
                        .setText(R.id.tv_send,"接受")
                        .fullWidth()
                        .fromBottom(false)
                        .show();

                final TextView tv=dialog.getView(R.id.tv_send);

                dialog.setOnClickListener(R.id.iv_photo, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this,"点击了头像"+tv.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                });*/


                PictureSelector.create(BannerViewActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .forResult(PictureConfig.CHOOSE_REQUEST);

            }
        });

        DefaultNavigation navigationBar = new DefaultNavigation.Builder(this)
                .setLeftIcon(R.mipmap.ic_launcher).setTitle("投稿").create();




    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);

                    Bitmap map=BitmapFactory.decodeFile(selectList.get(0).getPath());

                    iv_image.setImageBitmap(map);


                    break;
            }
        }

    }
}
